﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class warehouseDistribute : BaseAdmin
    {
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        readonly DALWarehouseGoods theWarehouseGoods = new DALWarehouseGoods();
        readonly DALGoods theGoods = new DALGoods();
        readonly DALModelType theModelType = new DALModelType();
        tbModelType oModelType;
        tbWarehouse oWarehouse;
        tbGoods oGoods;

        tbWarehouseGoods oWarehouseGoods;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehouseDistributeManager);
                this.BindWarehouse();
                this.BuildForm();
            }
        }

        void BuildForm()
        {
            oWarehouse = theWarehouse.GetModel(int.Parse(Request.QueryString["wid"]));
            if (oWarehouse != null)
            {
                this.lblWarehouseName.Text = oWarehouse.warehouseName;
            }
            oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
            if (oGoods != null)
            {
                this.lblGoodsName.Text = oGoods.goodsName;
                this.lblGoodsNo.Text = oGoods.goodsCode;
            }
            this.lblBatchNo.Text = Request.QueryString["bno"];
            this.lblGoodsCount.Text = Request.QueryString["gcount"];
            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                oModelType = theModelType.GetModel( int.Parse(Request.QueryString["mid"]));
                if (oModelType != null)
                {
                    this.lblModelTypeName.Text = oModelType.modelTypeName;
                }
            }
            

        }

        void BindWarehouse()
        {
            DataTable dt = theWarehouse.GetList("  id != " + Request.QueryString["wid"]).Tables[0];
            if (dt != null)
            {
                this.ddlInWarehouse.DataTextField = "warehouseName";
                this.ddlInWarehouse.DataValueField = "id";
                this.ddlInWarehouse.DataSource = dt;
                this.ddlInWarehouse.DataBind();
            }
            this.ddlInWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", "-1"));

        }


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (int.Parse(this.lblGoodsCount.Text) < int.Parse(this.txtGoodsCount.Text.Trim()))
            {
                ExtAspNet.Alert.Show("超出最大调拨数量限制！");
                return;
            }


            oWarehouseGoods = new tbWarehouseGoods();

            oWarehouseGoods.creater = int.Parse(base.AdminUserId);
            oWarehouseGoods.createTime = DateTime.Now;
            oWarehouseGoods.GoodsBatchNo = this.lblBatchNo.Text;
            oWarehouseGoods.goodsCount = -int.Parse(this.txtGoodsCount.Text);
            oWarehouseGoods.goodsId = int.Parse(Request.QueryString["gid"]);
            oWarehouseGoods.remark = "调拨出库";
            oWarehouseGoods.warehouseId = int.Parse(Request.QueryString["wid"]);
            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                oWarehouseGoods.ModelTypeId = int.Parse(Request.QueryString["mid"]);
            }
            theWarehouseGoods.Add(oWarehouseGoods);


            oWarehouseGoods = new tbWarehouseGoods();
            oWarehouseGoods.creater = int.Parse(base.AdminUserId);
            oWarehouseGoods.createTime = DateTime.Now;
            oWarehouseGoods.GoodsBatchNo = this.lblBatchNo.Text;
            oWarehouseGoods.goodsCount = int.Parse(this.txtGoodsCount.Text);
            oWarehouseGoods.goodsId = int.Parse(Request.QueryString["gid"]);
            oWarehouseGoods.remark = this.txtRemark.Text.Trim();
            oWarehouseGoods.warehouseId = int.Parse(this.ddlInWarehouse.SelectedItem.Value);
            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                oWarehouseGoods.ModelTypeId = int.Parse(Request.QueryString["mid"]);
            }
            theWarehouseGoods.Add(oWarehouseGoods);

            ExtAspNet.Alert.Show("调拨成功！", "", "window.location.href='warehouseDistributeList.aspx';");

        }
    }
}